<?
/**
**票付通对外接的全部在这里处理
**2015-04-10 青岛海底世界
**2015-04-30 携程
**2015-05-11 爱森斯
**2015-05-29 江西瑞金
**by pzp
**/

if (!class_exists("go_sql")){
	include("/var/www/html/new/conf/le.je");
	$le=new go_sql();
	$le->connect();
}

// include 'QD_underwater_world/qd_class.php';
// include 'AiSiSen/aisisen_class.php';
// include 'ctrip/ctrip_class.php';

$ac='16ucom';
$pw='c33367701511b4f6020ec61ded352059';
$inSide = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));
$txt=file_get_contents('php://input');
$log = logfile(date('Y-m-d H:i:s').'---'.$txt,'all_api_data2.txt');

$retime1 = microtime(true); 

$arr = base64_decode($txt);
$arrs1 = explode('#',$arr);
$arrs2 = explode('|',$arrs1[1]);
$method = $arrs1[0];
$Check = $arrs2[0];
$order_arr = json_decode($arrs2[1],true);
$order_arr_id = $order_arr[0]['id'];
$order = $order_arr[0]['orderid'];
$methoda = substr($arr,0,5);
$sign = md5(($order_arr_id + $order).$methoda); 
 
// if($order == 1152117 || $order == 1152137) exit;
if($Check == $sign){
	foreach($order_arr as $orders){ 
		$order_true = $orders['orderid'];
		$pft_order = $inSide->Order_Globle_Search(
								0,0,0,0,"",
								"","","","","",
								"","",$order_true,"",0,
								"","","",1,0,
								0,1,"","","",
								"","","","","",
								"","","1","","");
		$pft_order_xml =simplexml_load_string($pft_order);
		$lid = (int)$pft_order_xml->Rec->UUlid;
		$tid = (int)$pft_order_xml->Rec->UUtid;	
		$memo = (string)$pft_order_xml->Rec->UUremotenum;
		$UUpid=(int)$pft_order_xml->Rec->UUpid;
		$num = (int)$pft_order_xml->Rec->UUtnum;
		$status = (int)$pft_order_xml->Rec->UUstatus;
		$UUpaystatus = (int)$pft_order_xml->Rec->UUpaystatus;  //0景区到付1成功2未支付
		$UUpmode = (int)$pft_order_xml->Rec->UUpmode; //1支付宝|2使用分销余额|3信用支付|4到付
		if($UUpaystatus == 0 || $UUpmode ==4 )
			$ispay = 0;
		else $ispay = 1;			
		
		$UUlprice = $inSide->get_Dynamic_Price_Merge(
				$UUpid,(string)$pft_order_xml->Rec->UUbegintime,1,'','',1,0
		);  //零售价
		
		/*action值：TK79,Push推送订单,MD89,Modify修改订单,CC43,Cancel取消订单,*/
		switch ($method){
			case 'TK79':$action = 'Push';
			break;
			case 'MD89':$action = 'Revoke';
			break;
			case 'CC43':$action = 'Revoke';
			break;
			default:
			break;
		}
		
		$select_uuid = "select uuid from uu_jq_ticket where id=".$tid." limit 1";
		$GLOBALS['le']->query($select_uuid);
		$GLOBALS['le']->fetch_assoc();
		$uuid = $GLOBALS['le']->f('uuid');

		if($action=='Push'){ 
			$request_array = array(
				'pftordernum' =>$order_true,
				'pftordername' =>(string)$pft_order_xml->Rec->UUordername,
				'UUtnum' => (int)$pft_order_xml->Rec->UUtnum,
				'UUordertime'=>(string)$pft_order_xml->Rec->UUordertime,
				'UUplaytime'=>(string)$pft_order_xml->Rec->UUbegintime,
				'UUendtime'=>(string)$pft_order_xml->Rec->UUendtime,
				'UUtprice'=>((string)$pft_order_xml->Rec->UUtprice)/100,
				'UUordertel'=>(string)$pft_order_xml->Rec->UUordertel,
				'uuid'=>$uuid,	
				'IDcar'=>(string)$pft_order_xml->Rec->UUpersonid,
				'productname'=>(string)$pft_order_xml->Rec->UUttitle,
				'UUltitle'=>(string)$pft_order_xml->Rec->UUltitle,
				'UUcode'=>(string)$pft_order_xml->Rec->UUcode,
				'ispay' => $ispay,
			);
			$request_array['UUlprice'] = $UUlprice / 100;
			switch ($lid){
				case 5057: 
				case 8813:  //微信及短信通知
					include_once('/var/www/html/new/d/class/OrderSmsManual.class.php');
					$smsManual = new \PFT\SMS\OrderSmsManual($inSide);
					$proInfos['mainOrder'] = $pft_order_xml->Rec;
					$note = $smsManual->OrderNotify('AFTER_PAY', $proInfos);
					if($note == 1) return 100;
				break;	
				case 7641:   //青岛海底世界的景区订单
					include 'QD_underwater_world/qd_class.php';
					$QD_world = new QD_class();  //青岛海底世界
					$request = $QD_world->PostOrder($request_array);
					if($request == 101){ 
						echo 101;
					}
					else{
						$updateRemote = $inSide->Remote_Ordernum_Update(
							serialize(array($order_true=>$request['resultno']))
						);
						echo 100;
					}
				break;
				case 8062:  //鸡冠洞的景区订单
				case 8085:  //重渡沟的景区订单
					include 'AiSiSen/aisisen_class.php';
					$ass = new AiSiSen($lid);  //爱森斯
					$request = $ass->agencyOrderAdd($request_array);
					if($request == 101){ 
						echo 101;
					}
					else{
						$updateRemote = $inSide->Remote_Ordernum_Update(
							serialize(array($order_true=>(string)$request->platOrderNo))
						);
						echo 100;
					}
				break;
				case 8523:	//携程分销景区太姥山
					include 'ctrip/ctrip_class.php';
					$ctrip = new Ctrip();  //携程分销
					$uuids = explode("_",$uuid);
					$ctrip_pid = $uuids[0];
					$ctrip_rid = $uuids[1];
					$request_array['ContactName']='票付通';
					$request_array['ProductID']=$ctrip_pid;
					$request_array['TicketID']=$ctrip_rid;
					$request_array['UseDate']=(string)$pft_order_xml->Rec->UUbegintime;
					$request_array['Uid']='69433ba5-d56b-4a44-b1b8-07c9758d52cd';
					$request = $ctrip->TicketOrderCreateForPrepay_V2($request_array);
					if($request == 100) echo 100;
					else echo 101;
				break;
				case 8048:  //瑞金-叶坪
				case 8055:  //瑞金-二苏大
				case 8054:  //瑞金-红井
					include_once('RuiJin/ruijin.class.php');
					$RJ = new RunJin($lid);
					$request = $RJ->submitOrder($request_array);
					if($request == 101){ 
						echo 101;
					}
					else{
						$str = "update uu_order_fx_details set memo=100 where orderid=".$order_true." limit 1";
						$GLOBALS['le']->query($str);
						echo 100;
					}
				break;
				case 9104: //畅游通的测试景点
					include 'ChangYouTong/cyt_class.php';
					$cyt = new ChangYouTong(); 
					$request = $cyt->createPaymentOrder($request_array);
					if($request == 101) echo 101;
					else{
						
						$updateRemote = $inSide->Remote_Ordernum_Update(
							serialize(array($order_true=>(string)$request->partnerorderId)));
						echo 100;
					}
				break;
				default:
				break;
			} 
		}elseif($action == 'Revoke'){ //修改and取消
			switch ($lid){
				case 7641:
					include 'QD_underwater_world/qd_class.php';
					$QD_world = new QD_class();  //青岛海底世界
					$select_Onum = $QD_world->GetOrderByOrderNo($order_true);
					$QD_Onum = $select_Onum['Gdetail'][0]['ProductNum'];  //青岛系统的订单数量 
					$QD_ProductID = $select_Onum['Gdetail'][0]['ProductID'];
					if($status == 0){ //修改
						$type = 2; //2是修改数量
						$rnum = $QD_Onum - $num; //需要退款的数量
					}elseif($status == 3){//取消
						$type = 3; //3是申请退票
						$rnum = $QD_Onum;
					}
					if($QD_Onum > 0 && $rnum > 0){
						$modify_array = array(
							'order'=> $order_true,
							'type' => $type, 
							'rnum' => $rnum,
							'memo' =>$memo,
							'ProductID'=>$QD_ProductID,
						);
						$request_modify = $QD_world->EditOrder($modify_array);
						if($request_modify == 101) echo 101;  //修改失败
						else	echo 100;	//修改成功
					}else echo 101;
				break;
				case 8062:  //鸡冠洞的景区订单
				case 8085:  //重渡沟的景区订单
					include 'AiSiSen/aisisen_class.php';
					$ass = new AiSiSen($lid);  //爱森斯
					if($status == 0){ //修改
						$editMode =2;//2是修改数量
					}elseif($status == 3){//取消
						$editMode = 1; //1是整单全退
					}
					$modify_array = array(
						'editMode' => $editMode, 
						'productNum' => $num,
						'platOrderNo' =>$memo,
						'productCode'=>$uuid,
					);
					$request_modify = $ass->EditOrderNum($modify_array);
					if($request_modify == 101) echo 101;  //修改失败
					else	echo 100;	//修改成功
				break;
				case 8523:  //携程订单
					include 'ctrip/ctrip_class.php';
					$ctrip = new Ctrip();  //携程分销
					if($status == 3){//取消
						$request_modify = $ctrip->TicketUnsubscribeOrder($memo);
					}
					if($request_modify == 101) echo 101;  //修改失败
					else	echo 100;	//修改成功
				break;
				case 9104: //畅游通的测试景点
					include 'ChangYouTong/cyt_class.php';
					$cyt = new ChangYouTong();
					$cyt_modify = array(
						'partnerorderId'=>$memo,
						'refundSeq'=>$UUcode,
						'orderRefundCharge'=>0,
						'refundExplain'=>'',
						'name'=>$pftordername,
						'credentials'=>$IDcar,					
					);
					if($status == 3){ //取消
						$cyt_modify['orderPrice']=($UUtnum * $UUlprice)*100;
						$cyt_modify['orderQuantity']=$UUtnum;
						$cyt_modify['refundQuantity']=$UUtnum;
						$cyt_modify['orderRefundPrice']=($UUtnum * $UUlprice)*100;
						$cyt_modify['rectime']=$UUctime;						
						$request = $cyt->applyOrderRefundByUser($cyt_modify);
					}elseif($status == 0){ //修改
						$select_ordernum = "select pre_num,done_num,rectime from pft_order_num_modify where orderid=".$order_true." order by id desc limit 1";
						$GLOBALS['le']->query($select_ordernum);
						$GLOBALS['le']->fetch_assoc();
						$pre_num = $GLOBALS['le']->f('pre_num');
						$done_num = $GLOBALS['le']->f('done_num');
						$rectime = $GLOBALS['le']->f('rectime');
						$cyt_modify['orderPrice']=($pre_num * $UUlprice)*100;
						$cyt_modify['orderQuantity']=$pre_num;
						$cyt_modify['refundQuantity']=$pre_num-$done_num ;
						$cyt_modify['orderRefundPrice']=(($pre_num-$done_num) * $UUlprice)*100;
						$cyt_modify['rectime']=$rectime;
						$request = $cyt->applyOrderRefundByUser($cyt_modify);
					}
					if($request == 101){						
						echo 101;						
					}
					else{ 
						$str = "update uu_order_fx_details set memo='cyt_".(string)$request->partnerorderId."_".(string)$request->orderStatus."' where orderid=".$order_true." limit 1";
						$GLOBALS['le']->query($str);
						echo 101;
					}
				break;
				case 8048:  //瑞金-叶坪
				case 8055:  //瑞金-二苏大
				case 8054:  //瑞金(九天)-红井
					if($status == 3){//取消
						include_once('RuiJin/ruijin.class.php');
						$RJ = new RunJin($lid);					
						$request = $RJ->removeOrder($order_true);
						if($request == 101){ 
							echo 101;
						}
						else{
							$str = "update uu_order_fx_details set memo='100_3' where orderid=".$order_true." limit 1";
							$GLOBALS['le']->query($str);
							echo 100;
						}
					}
				break;
				default;
				break;
			}
		
		}
	}
}

$retime2 = microtime(true);
$retime3 = $retime2 - $retime1 ;  
$log = logfile(date('Y-m-d H:i:s').' runtime:'.$retime3,'all_api_data.txt');
//
function logfile($txt,$path){
	$fp = fopen($path,"a");
	flock($fp, LOCK_EX) ;
	fwrite($fp,$txt."\n");
	flock($fp, LOCK_UN);
	fclose($fp); 
}

?>